"""
oemaker MCP Server

面向 oemaker 软件的 MCP 服务器，oemaker 是一个用于构建 DVD ISO 的工具，包括标准版 ISO、调试版 ISO 和源码版 ISO。

MCP tools 列表：
- oemaker_help: 显示 oemaker 帮助信息
  参数：无
  返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

- oemaker_build_standard: 构建标准版 ISO 镜像
  参数：
    target_platform: 目标平台名称
    os_version: 操作系统版本
    architecture: 系统架构
    repo_address: 软件源地址
  返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
"""

from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json

mcp = FastMCP("oemaker MCP Server")

@mcp.tool()
def oemaker_help() -> dict:
    """
    显示 oemaker 命令的帮助信息
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    try:
        result = subprocess.run(["oemaker", "-h"], capture_output=True, text=True, check=False)
        return {
            "success": result.returncode == 0,
            "command": "oemaker -h",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "oemaker -h",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def oemaker_build_standard(target_platform: str, os_version: str, architecture: str, repo_address: str) -> dict:
    """
    构建标准版 ISO 镜像
    
    参数：
        target_platform: 目标平台名称
        os_version: 操作系统版本
        architecture: 系统架构
        repo_address: 软件源地址
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    try:
        command = ["oemaker", "-t", "standard", "-p", target_platform, "-v", os_version, "-r", architecture, "-s", repo_address]
        result = subprocess.run(command, capture_output=True, text=True, check=False)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": f"oemaker -t standard -p {target_platform} -v {os_version} -r {architecture} -s {repo_address}",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()